package com.solming.leetcode.question;

import java.util.LinkedHashMap;

public class LRUCache146 {
    LinkedHashMap<Integer, Integer> cache;
    int capacity = 0;
    int size = 0;

    public LRUCache146(int capacity) {
        this.capacity = capacity;
        cache = new LinkedHashMap<>(capacity);
    }

    public int get(int key) {
        if (!cache.containsKey(key)) {
            return -1;
        }
        int val = cache.get(key);
        cache.remove(key);
        cache.put(key,val);
        return val;
    }

    public void put(int key, int value) {
        if (cache.containsKey(key)) {
            cache.remove(key);
            cache.put(key, value);

            return;
        }
        if (size >= capacity) {
            cache.remove(cache.keySet().iterator().next());
        }else{
            size++;
        }
        cache.put(key, value);

    }


}
